package com.sgcc.statistics.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.sgcc.common.core.domain.R;
import com.sgcc.common.core.utils.DateUtils;
import com.sgcc.statistics.domain.OConsPercentage;
import com.sgcc.statistics.service.IOConsPercentageService;
import com.sgcc.system.api.RemoteDeptService;
import com.sgcc.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import com.sgcc.statistics.mapper.CConsMapper;
import com.sgcc.statistics.domain.CCons;
import com.sgcc.statistics.service.ICConsService;
import org.springframework.stereotype.Service;

/**
 * CConsService业务层处理
 * 
 * @author sgcc
 * @date 2021-04-14
 */
@Service
public class CConsServiceImpl implements ICConsService 
{
    @Autowired
    private CConsMapper cConsMapper;

    @Autowired
    private RemoteDeptService remoteDeptService;

    @Autowired
    private IOConsPercentageService ioConsPercentageService;

    /**
     * 查询CCons
     * 
     * @param consId CConsID
     * @return CCons
     */
    @Override
    public CCons selectCConsById(Long consId)
    {
        return cConsMapper.selectCConsById(consId);
    }

    /**
     * 查询CCons列表
     * 
     * @param cCons CCons
     * @return CCons
     */
    @Override
    public List<CCons> selectCConsList(CCons cCons)
    {
        return cConsMapper.selectCConsList(cCons);
    }

    /**
     * 新增CCons
     * 
     * @param cCons CCons
     * @return 结果
     */
    @Override
    public int insertCCons(CCons cCons)
    {
        return cConsMapper.insertCCons(cCons);
    }

    /**
     * 修改CCons
     * 
     * @param cCons CCons
     * @return 结果
     */
    @Override
    public int updateCCons(CCons cCons)
    {
        return cConsMapper.updateCCons(cCons);
    }

    /**
     * 批量删除CCons
     * 
     * @param consIds 需要删除的CConsID
     * @return 结果
     */
    @Override
    public int deleteCConsByIds(Long[] consIds)
    {
        return cConsMapper.deleteCConsByIds(consIds);
    }

    /**
     * 删除CCons信息
     * 
     * @param consId CConsID
     * @return 结果
     */
    @Override
    public int deleteCConsById(Long consId)
    {
        return cConsMapper.deleteCConsById(consId);
    }

    @Override
    public void getHushuZb() {
        // 获取去年开始时间和结束时间 获取上一年的年份，然后拼接出开始时间和结束时间
        String year = DateUtils.getLastYear();
        String endTime = year + "-12-31 23:59:59";
        // 查询出省下面所有的地市
        R r = remoteDeptService.getUnit("35101");
        if (r.getCode() == 200){
            List<SysDept> sysDeptList = (List<SysDept>) r.getData();
            for (SysDept sysDept : sysDeptList){
                CCons hushuZb = cConsMapper.getHushuZb(sysDept.getDeptId());
                OConsPercentage oConsPercentage = new OConsPercentage();
                String id= UUID.randomUUID().toString().replaceAll("-", "");
                oConsPercentage.setId(id);
                oConsPercentage.setPercentage(hushuZb.gethNumPercentage());
                oConsPercentage.setQyPercentage(hushuZb.getQyhNumPercentage());
                oConsPercentage.setUserNum(hushuZb.getUserNum());
                oConsPercentage.setQyUserNum(hushuZb.getQyUserNum());
                oConsPercentage.setUnit(sysDept.getDeptId());
                oConsPercentage.setCdate(endTime.toString());
                oConsPercentage.setCreateTime(new Date());
                ioConsPercentageService.insertOConsPercentage(oConsPercentage);
            }
        }
    }

    @Override
    public void getCityHushuZb() {
        String year = DateUtils.getLastYear();
        String endTime = year + "-12-31 23:59:59";

        R r = remoteDeptService.getSubordinateUnit("35101");
        if (r.getCode() == 200){
            List<SysDept> sysDeptList = (List<SysDept>) r.getData();
            for (SysDept sysDept : sysDeptList){
                CCons hushuZb = cConsMapper.getCityHushuZb(sysDept.getOrgId(),sysDept.getParentId());
                OConsPercentage oConsPercentage = new OConsPercentage();
                String id= UUID.randomUUID().toString().replaceAll("-", "");
                oConsPercentage.setId(id);
                oConsPercentage.setPercentage(hushuZb.gethNumPercentage());
                oConsPercentage.setQyPercentage(hushuZb.getQyhNumPercentage());
                oConsPercentage.setUserNum(hushuZb.getUserNum());
                oConsPercentage.setQyUserNum(hushuZb.getQyUserNum());
                oConsPercentage.setUnit(sysDept.getDeptId());
                oConsPercentage.setCdate(endTime);
                oConsPercentage.setCreateTime(new Date());
                ioConsPercentageService.insertOConsPercentage(oConsPercentage);
            }
        }
    }


    /*public static void main(String[] args) {
        Date endDayOfLastMonth = DateUtils.getEndDayOfLastMonth();
        String endDate = endDayOfLastMonth.toString();
        String endtime = endDate.substring(0, 19);
        System.out.println("过去一年："+endtime);
    }*/
}
